package tcpFirmware.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBHelper {
    private static Connection connection=null;
    public static String url = "jdbc:sqlserver://192.168.137.95:1433;DatabaseName=MovieData";
    public static String username = "guan";
    public static String password = "123";
    public static boolean isConnected = false;

    private void getInstance(){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection(){
        if (isConnected){
            return connection;
        }else {
            getInstance();
            try {
                connection = DriverManager.getConnection(url,username,password);
                isConnected = true;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return connection;
    }

    public boolean closeConnection(){
        try {
            connection.close();
            isConnected=false;
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
}
